import {Injectable} from '@angular/core';
import {HttpServicesProvider} from "../http-services/http-services";
import {UtilsProvider} from "../utils/utils";
import {UrlProvider} from "../url/url";
import {ConfigProvider} from "../config/config";
/// <reference path="../assets/libs/wxsdk/wechat.d.ts">
@Injectable()
export class WechatProvider {
    shareData = {
        title: '',
        desc: '',
        link: '',
        imgUrl: '',
        success: ()=>{},
        cancel: ()=>{}
    };
    constructor(
        public httpService: HttpServicesProvider,
        public util:UtilsProvider,
        public url: UrlProvider,
        public config: ConfigProvider
    ) {
        // this.getWxConfig(()=>{
        //     this.wxShareMoments({shareNumber:0, shareTitle:'', amoyId: 0,userId :0});
        // });
    }

    shareInit(shareNumber, shareTitle='', amoyId=0,userId =0){
        let shareData = {};
        Object.assign(shareData, {imgUrl: this.config.shareImgUrl});
        // this.shareData.imgUrl = this.config.shareImgUrl;
        /**
         * sharePageNumber
         * 1: 信息详情
         * 2: 任务详情
         * 3: 餐饮故事
         * 4: 餐饮资讯
         * 5: 论坛
         * 0: 其他 (默认)
         * */
        
        let title = '';
        switch (shareNumber){
            case 1:
                title = shareTitle == '' || shareTitle == undefined ? "餐饮信息海量发（淘鲜生餐饮圈）" : shareTitle;
                Object.assign(shareData, {title: title});
                Object.assign(shareData, {desc: "频繁换群发信息？繁琐费事够憋屈！本土餐饮信息大平台--淘鲜生餐饮圈，信息海量发"});
                Object.assign(shareData, {link: this.url.getShareUrl('information_detail',{id:amoyId},userId)});
                break;
            case 2:
                title = shareTitle == '' || shareTitle == undefined ? "快来做佣丁，餐饮任务悬赏平台（淘鲜生餐饮圈）" : shareTitle;
                Object.assign(shareData, {title: title});
                Object.assign(shareData, {imgUrl: this.config.shareImgUrl1});
                Object.assign(shareData, {desc: "快来，有赏金：餐饮人脉/资源的悬赏/变现平台，餐饮业内资源互换，互帮互助还有赏金；"});
                Object.assign(shareData, {link: this.url.getShareUrl('post_detail',{id:amoyId},userId)});
                break;
            case 3:
                title = shareTitle == '' || shareTitle == undefined ? "餐饮人招聘、求职、顶岗，出租、转让、二手买卖；吐槽、求助、讲述咱们自己的餐饮故事！" : shareTitle;
                Object.assign(shareData, {title: title});
                Object.assign(shareData, {desc: "淘鲜生餐饮圈，餐饮人的聚集地"});
                Object.assign(shareData, {link: this.url.getShareUrl('article_detail',{id:amoyId},userId)});
                break;
            case 4:
                title = shareTitle == '' || shareTitle == undefined ? "餐饮人招聘、求职、顶岗，出租、转让、二手买卖；吐槽、求助、讲述咱们自己的餐饮故事！" : shareTitle;
                Object.assign(shareData, {title: title});
                Object.assign(shareData, {desc: "本土的、世界的，家乡的餐饮故事、前沿的餐饮资讯"});
                Object.assign(shareData, {link: this.url.getShareUrl('article_detail',{id:amoyId},userId)});
                break;
            case 5:
                title = shareTitle == '' || shareTitle == undefined ? "餐饮人招聘、求职、顶岗，出租、转让、二手买卖；吐槽、求助、讲述咱们自己的餐饮故事！" : shareTitle;
                Object.assign(shareData, {title: title});
                Object.assign(shareData, {desc: "本土餐饮论坛：倾诉、唠嗑、吐槽、爆料、灌水、求助"});
                Object.assign(shareData, {link: this.url.getShareUrl('service_bbs',{id:0},userId)});
                break;
            default:
                Object.assign(shareData, {title: "淘鲜生餐饮圈（山西本土餐饮平台）"});
                Object.assign(shareData, {desc: "山西本土餐饮人的信息发布平台、招聘求职平台、门店出转平台、任务悬赏平台、餐饮生活社交平台"});
                Object.assign(shareData, {link: location.href});
        }
        return shareData;
    }

    getWxConfig(callback=()=>{}) {
        this.httpService.requestPostData('/wechat/get_wx_config', {
            url: location.href.split("#")[0]
        }, response => {
            let config = response['info'];
            wx.config({
                // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: config['appid'], // 必填，公众号的唯一标识
                timestamp: config['time'], // 必填，生成签名的时间戳
                nonceStr: config['noncestr'], // 必填，生成签名的随机串
                signature: config['signature'],// 必填，签名
                jsApiList: ['chooseWXPay','onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填，需要使用的JS接口列表
            });
            wx.ready(function(){
                callback();
            });
        });
    }
    
    wxPay(data,callback){
        this.httpService.requestPostData('/echarge/recharge', data, response => {
            let config = response['pay_code'];
            wx.chooseWXPay({
                timestamp: config['timeStamp'], // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                nonceStr: config['nonceStr'], // 支付签名随机串，不长于 32 位
                package: config['package'], // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                signType: config['signType'], // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                paySign: config['paySign'], // 支付签名
                success: function (res) {
                    localStorage.setItem('wxPaySuccessInfo', JSON.stringify(res));
                    callback(true);
                }
            });
        });
    }



    wxShareMoments({shareNumber, shareTitle, amoyId , userId}, success=()=>{
    
    },cancel=()=>{}){
        let shareData = this.shareInit(shareNumber, shareTitle, amoyId , userId);
        Object.assign(shareData, {success: success}, {cancel: cancel});
        wx.onMenuShareTimeline(shareData);
        
    }


    wxShareFriend({shareNumber, shareTitle, amoyId, userId},success=()=>{},cancel=()=>{}){
        let shareData = this.shareInit(shareNumber, shareTitle, amoyId , userId);
        Object.assign(shareData, {success: success}, {cancel: cancel});
        wx.onMenuShareAppMessage(shareData);
    }

}
